import { defineStore } from 'pinia'
import { ref } from 'vue'
import { 
  getDiscoverPosts, 
  getUserPosts, 
  createPost, 
  getPostDetail,
  deletePost,
  getPostComments,
  getUserCollections,
  addComment,
  likePost,
  unlikePost,
  collectPost,
  uncollectPost,
  likeComment,
  unlikeComment
} from '@/api/post'

export const usePostStore = defineStore('post', () => {
  const discoverPosts = ref([])
  const userPosts = ref([])
  const currentPost = ref(null)
  const isLoading = ref(false)
  const hasMore = ref(true)
  const currentPage = ref(1)

  // 获取发现页帖子
  const fetchDiscoverPosts = async (params = {}) => {
    try {
      isLoading.value = true
      const response = await getDiscoverPosts({
        ...params,
        page: currentPage.value
      })
      
      const { items, total, page, limit } = response.data
      
      if (page === 1) {
        discoverPosts.value = items
      } else {
        discoverPosts.value = [...discoverPosts.value, ...items]
      }
      
      hasMore.value = items.length === limit && discoverPosts.value.length < total
      if (hasMore.value) {
        currentPage.value++
      }
      
      return response
    } catch (error) {
      console.error('获取发现页帖子失败', error)
      throw error
    } finally {
      isLoading.value = false
    }
  }

// 获取用户帖子列表
const fetchUserPosts = async (userId, params = {}) => {
  try {
    isLoading.value = true
    const response = await getUserPosts(userId, params)
    userPosts.value = response.data.items || response.data.list || []
    return response
  } catch (error) {
    console.error('获取用户帖子失败', error)
    throw error
  } finally {
    isLoading.value = false
  }
}

// 获取用户收藏列表
const fetchUserCollections = async (userId, params = {}) => {
  try {
    isLoading.value = true
    const response = await getUserCollections(userId, params)
    userCollections.value = response.data.items || response.data.list || []
    return response
  } catch (error) {
    console.error('获取用户收藏失败', error)
    throw error
  } finally {
    isLoading.value = false
  }
}

  // 发布帖子
  const publishPost = async (postData) => {
    try {
      const response = await createPost(postData)
      // 将新帖子添加到用户帖子列表开头
      userPosts.value.unshift(response.data)
      return response
    } catch (error) {
      throw error
    }
  }

  // 获取帖子详情
  const fetchPostDetail = async (postId) => {
    try {
      const response = await getPostDetail(postId)
      currentPost.value = response.data
      return response
    } catch (error) {
      console.error('获取帖子详情失败', error)
      throw error
    }
  }

  // 删除帖子
  const removePost = async (postId) => {
    try {
      await deletePost(postId)
      // 从帖子列表中移除
      userPosts.value = userPosts.value.filter(post => post.id !== postId)
      discoverPosts.value = discoverPosts.value.filter(post => post.id !== postId)
    } catch (error) {
      throw error
    }
  }

  // 获取帖子评论
  const fetchPostComments = async (postId, params = {}) => {
    try {
      const response = await getPostComments(postId, params)
      return response.data
    } catch (error) {
      throw error
    }
  }

  // 添加评论
  const submitComment = async (postId, data) => {
    try {
      const response = await addComment(postId, data)
      return response.data
    } catch (error) {
      throw error
    }
  }

  // 点赞/取消点赞帖子
  const togglePostLike = async (postId, isLiked) => {
    try {
      if (isLiked) {
        await unlikePost(postId)
      } else {
        await likePost(postId)
      }
      return true
    } catch (error) {
      throw error
    }
  }

  // 收藏/取消收藏帖子
  const togglePostCollect = async (postId, isCollected) => {
    try {
      if (isCollected) {
        await uncollectPost(postId)
      } else {
        await collectPost(postId)
      }
      return true
    } catch (error) {
      throw error
    }
  }

  // 点赞/取消点赞评论
  const toggleCommentLike = async (commentId, isLiked) => {
    try {
      if (isLiked) {
        await unlikeComment(commentId)
      } else {
        await likeComment(commentId)
      }
      return true
    } catch (error) {
      throw error
    }
  }

  // 重置状态
  const resetPosts = () => {
    discoverPosts.value = []
    userPosts.value = []
    currentPost.value = null
    isLoading.value = false
    hasMore.value = true
    currentPage.value = 1
  }

  return {
    discoverPosts,
    userPosts,
    currentPost,
    isLoading,
    hasMore,
    fetchDiscoverPosts,
    fetchUserPosts,
    publishPost,
    fetchPostDetail,
    removePost,
    fetchPostComments,
    submitComment,
    togglePostLike,
    togglePostCollect,
    toggleCommentLike,
    resetPosts,
    fetchUserCollections
  }
})